<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="单据编号" prop="vBillNo">
        <el-input
          v-model="queryParams.vBillNo"
          placeholder="请输入单据编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="邮递编号" prop="vPostNo">
        <el-input
          v-model="queryParams.vPostNo"
          placeholder="请输入邮递编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="邮递方式" prop="vPostType">
        <el-select
          v-model="queryParams.vPostType"
          placeholder="请选择邮递方式"
          clearable
        >
          <el-option
            v-for="dict in dict.type.buy_post_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择状态"
          clearable
        >
          <el-option
            v-for="dict in dict.type.inv_arrival_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          >删除</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          >导出</el-button
        >
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="postList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="单据编号" align="center" prop="vBillNo" />
      <el-table-column label="邮递编号" align="center" prop="vPostNo" />
      <el-table-column label="邮递方式" align="center" prop="vPostType">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.buy_post_type"
            :value="scope.row.vPostType"
          />
        </template>
      </el-table-column>
      <el-table-column label="经办人" align="center" prop="oprBy" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.inv_arrival_status"
            :value="scope.row.status"
          />
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleView(scope.row)"
            >物流明细</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改采购邮寄对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="单据编号" prop="vBillNo">
          <el-input
            v-model="form.vBillNo"
            placeholder="请输入单据编号"
            disabled="true"
          />
        </el-form-item>
        <el-form-item label="对应合同" prop="vBillNos">
          <el-tag
            :key="tag"
            v-for="tag in dynamicTags"
            :disable-transitions="false"
          >
            {{ tag }}
          </el-tag>
        </el-form-item>
        <el-form-item label="邮递编号" prop="vPostNo">
          <el-input v-model="form.vPostNo" placeholder="请输入邮递编号" />
        </el-form-item>
        <el-form-item label="邮递方式" prop="vPostType">
          <el-select v-model="form.vPostType" placeholder="请选择邮递方式">
            <el-option
              v-for="dict in dict.type.buy_post_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="经办人" prop="oprBy">
          <el-input v-model="form.oprBy" placeholder="请输入经办人" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态">
            <el-option
              v-for="dict in dict.type.inv_arrival_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog
      title="物流明细"
      :visible.sync="viewOpen"
      width="800px"
      append-to-body
    >
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="viewAdd"
            >新增明细</el-button
          >
        </el-col>
      </el-row>
      <el-table height="500px" :data="detailList">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="合同号" align="center" prop="vBillNo" />
        <el-table-column label="项目号" align="center" prop="vProjectNo" />
        <el-table-column label="物料代码" align="center" prop="vSpa" />
        <el-table-column label="物料名称" align="center" prop="vSpaName" />
        <el-table-column label="规格型号" align="center" prop="vSpaModel" />
        <el-table-column label="本次物流数量" align="center" prop="nQty">
          <template slot-scope="scope">
            <el-input size="small" v-model="scope.row.nQty"></el-input>
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="detailSubmit">确 定</el-button>
        <el-button>取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog
      title="新增明细"
      :visible.sync="detailOpen"
      width="800px"
      append-to-body
    >
      <el-table
        height="500px"
        :data="addList"
        @selection-change="detailSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="合同号" align="center" prop="vBillNo" />
        <el-table-column label="项目号" align="center" prop="vProjectNo" />
        <el-table-column label="物料代码" align="center" prop="vSpa" />
        <el-table-column label="物料名称" align="center" prop="vSpaName" />
        <el-table-column label="规格型号" align="center" prop="vSpaModel" />
        <el-table-column label="未发货数量" align="center" prop="nQty">
          <!-- <template slot-scope="scope">
            <el-input size="small" v-model="scope.row.nQty"></el-input>
          </template> -->
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="addSubmit">确 定</el-button>
        <el-button>取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listPost,
  getPost,
  delPost,
  addPost,
  updatePost,
} from "@/api/buy/post";
import { getNotPostItem, getPostItem, savePostItem } from "@/api/buy/buy0003";

export default {
  name: "Post",
  dicts: ["sys_common_status", "buy_post_type", "inv_arrival_status"],
  data() {
    return {
      // 遮罩层
      loading: true,
      rowContractNos: "",
      viewOpen: false,
      detailOpen: false,
      dynamicTags: [],
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 采购邮寄表格数据
      postList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        nMainId: null,
        vBillNo: null,
        vPostNo: null,
        vPostType: null,
        oprBy: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      addList: [],
      details: [],
      detailList: [],
      rowiD: "",
    };
  },
  created() {
    this.dynamicTags = this.$route.params.vBillNos;
    this.getList();
  },
  methods: {
    /** 查询采购邮寄列表 */
    getList() {
      this.loading = true;
      this.queryParams.vContractNos = this.$route.params.vBillNos.join(",");
      listPost(this.queryParams).then((response) => {
        this.postList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        nMainId: null,
        vBillNo: null,
        vPostNo: null,
        vPostType: null,
        oprBy: this.$store.state.user.name,
        status: "00",
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    detailSelectionChange(selection) {
      this.details = selection.map((item) => item);
      console.log(this.details);
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加采购邮寄";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getPost(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改采购邮寄";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updatePost(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            var vContractNos = this.dynamicTags.join(",");
            this.form.vContractNos = vContractNos;
            addPost(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除采购邮寄编号为"' + ids + '"的数据项？')
        .then(function () {
          return delPost(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/post/export",
        {
          ...this.queryParams,
        },
        `post_${new Date().getTime()}.xlsx`
      );
    },
    handleView(row) {
      this.detailList = [];
      this.rowiD = row.id;
      this.rowContractNos = row.vContractNos;
      getPostItem({ id: row.id }).then((response) => {
        this.detailList = response.data;
      });
      this.viewOpen = true;
    },
    viewAdd() {
      getNotPostItem(this.rowContractNos).then((response) => {
        this.addList = response.data;
      });
      this.detailOpen = true;
      this.details = [];
    },
    addSubmit() {
      this.details.forEach((item) => {
        if (this.detailList.length == 0) {
          this.detailList.push(item);
        } else {
          let obj = this.detailList.find((detailItem) => {
            return (
              item.vSpa == detailItem.vSpa &&
              item.vSpaName == detailItem.vSpaName &&
              item.vSpaModel == detailItem.vSpaModel &&
              item.vSpaBrand == detailItem.vSpaBrand
            );
          });
          if (obj) {
            obj.nQty = obj.nQty + item.nQty;
          } else {
            this.detailList.push(item);
          }
        }
      });

      this.detailOpen = false;
    },
    detailSubmit() {
      this.detailList.forEach((detailItem) => {
        detailItem.nMainId = this.rowiD;
        detailItem.vContractNo = detailItem.vBillNo;
      });
      console.log(this.rowiD);

      savePostItem(this.detailList).then((response) => {
        this.$modal.msgSuccess("新增成功");
        this.viewOpen = false;
        this.getList();
      });
    },
  },
};
</script>
<style>
.el-tag + .el-tag {
  margin-left: 10px;
}
</style>
